﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ArivisTests.Models;

namespace ArivisTests.Controllers
{
    [Authorize]
    public class TestRunController : Controller
    {
        private ArivisTestsDBContext db = new ArivisTestsDBContext();

        //
        // GET: /TestRun/

        public ActionResult Index()
        {
            return View(db.TestRuns.ToList());
        }

        //
        // GET: /TestRun/Details/5

        public ActionResult Details(int id = 0)
        {
            var testRuns = db.TestRuns.Include(run => run.Tests.Select(test => test.Scenario));
            testRuns = testRuns.Include(run => run.Tests.Select(test => test.Result));
            testRuns = testRuns.Include(run => run.Tests.Select(test => test.User));

            ViewBag.Results = db.TestResults;
            ViewBag.Users = db.ActiveTester;

            TestRun testrun = testRuns.FirstOrDefault(x => x.Id == id);
            if (testrun == null)
            {
                return HttpNotFound();
            }
            return View(testrun);
        }

        //
        // GET: /TestRun/Create

        public ActionResult Create()
        {
            ViewBag.Plans = db.TestPlans.Select(this.ToListItem);
            ViewBag.Tester = db.ActiveTester.Select(this.ToListItem);
            
            TestRun testrun = new TestRun();
            testrun.Date = DateTime.Today;
            testrun.Active = true;
            return View(testrun);
        }

        private SelectListItem ToListItem(TestPlan testplan)
        {
            return new SelectListItem() { Value = testplan.Id.ToString(), Text = testplan.Name };
        }
        private SelectListItem ToListItem(UserProfile userProfile)
        {
            return new SelectListItem() { Value = userProfile.UserId.ToString(), Text = userProfile.UserName };
        }

        //
        // POST: /TestRun/Create

        [HttpPost]
        public ActionResult Create(TestRun testrun, int? selectedPlan)
        {
            if (ModelState.IsValid)
            {
                // create test cases from selected plan
                db.TestRuns.Add(testrun);
                testrun.Tests = GetScenarios(selectedPlan).Select(x => TestCase.Create(db, x)).ToList();

                db.SaveChanges();
                return RedirectToAction("Details", new { id = testrun.Id });
            }

            return View(testrun);
        }

        private ICollection<TestScenario> GetScenarios(int? selectedPlan)
        {
            // empty list if no selected plan
            if (selectedPlan.HasValue == false)
                return new List<TestScenario>();

            // empty list if no selected plan
            var testPlan = db.TestPlans.FirstOrDefault(plan => plan.Id == selectedPlan.Value);
            if (testPlan == null)
                return new List<TestScenario>();

            return testPlan.Tests;
        }

        //
        // GET: /TestRun/Edit/5
        public ActionResult Edit(int id = 0)
        {
            TestRun testrun = db.TestRuns.Find(id);
            if (testrun == null)
            {
                return HttpNotFound();
            }
            return View(testrun);
        }

        //
        // POST: /TestRun/Edit/5

        [HttpPost]
        public ActionResult Edit(TestRun testrun, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                // update test run
                var dbTestRun = db.TestRuns.First(x => x.Id == testrun.Id);
                dbTestRun.Name = testrun.Name;
                dbTestRun.Date = testrun.Date;
                dbTestRun.Active = testrun.Active;

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(testrun);
        }

        //
        // GET: /TestRun/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TestRun testrun = db.TestRuns.Find(id);
            if (testrun == null)
            {
                return HttpNotFound();
            }
            return View(testrun);
        }

        //
        // POST: /TestRun/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            TestRun testrun = db.TestRuns.Find(id);
            foreach (var testCase in testrun.Tests.ToList())
            {
                db.TestCases.Remove(testCase);
            }
            db.TestRuns.Remove(testrun);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult RemoveTest(int id, int[] testcase)
        {
            if (testcase == null)
                return RedirectToAction("Details", new { id = id });

            var toRemove = db.TestCases.Where(x => testcase.Contains(x.Id));
            foreach(var item in toRemove)
            {
                db.TestCases.Remove(item);
            }
            db.SaveChanges();
            return RedirectToAction("Details", new { id = id });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}